import { formatDateTime } from '@fl/utils/fn.ts'
import { cloneDeep } from 'lodash-es'
import { reactive } from 'vue'

export function useTable() {
    const gridOptions = reactive<any>({
        border: true,
        columnConfig: {
            resizable: true,
        },
        columns: [
            // { slots: { checkbox: 'checkbox_cell', header: 'checkbox_header' }, type: 'checkbox', width: 60 },
            // { field: 'projectId', title: '楼盘ID', width: 120 },
            { field: 'projectName', slots: { default: 'buildingName' }, title: '楼盘名称', width: 160 },
            { field: 'area', minWidth: 100, title: '区域' },
            // { field: 'projectTag', minWidth: 120, title: '楼盘基础标签' },
            { field: 'businessTag', minWidth: 100, title: '业务标签' },
            { field: 'themeTag', minWidth: 120, title: '楼盘主题' },
            { field: 'policyTag', minWidth: 80, title: '政策标签' },
            { field: 'couponTag', minWidth: 80, title: '优惠标签' },
            { field: 'soonTag', minWidth: 100, slots: { default: 'soonStatus' }, title: '即将开盘' },
            { field: 'otherTag', minWidth: 100, title: '其他' },
            { fixed: 'right', slots: { default: 'operate' }, title: '操作', width: 160 },
        ],
        data: [],
        editConfig: {
            mode: 'row',
            showStatus: true,
            trigger: 'manual',
        },
        height: 576,
        keepSource: true,
        loading: false,
        pagerConfig: {
            currentPage: 1,
            enabled: true,
            pageSize: 10,
            pageSizes: [10, 20, 50, 100, 200, 500],
            total: 0,
        },
        showOverflow: true,
        stripe: true,
    })

    const btns = [
        {
            label: '查看',
            text: true,
            value: 'view',
        },
        {
            label: '编辑',
            text: true,
            value: 'edit',
        },
        /**
             {
            label: '删除',
            text: true,
            value: 'delete',
        }
        */
    ]

    const addBtn: any = {
        label: '新建',
        text: true,
        value: 'add',
    }

    // 删除空属性
    const paramsIsNull = (obj: any) => {
        const _newPar = {}
        for (const key in obj) {
            if (
                (obj[key] === 0 || obj[key])
                && obj[key].toString().replace(/(^\s*)|(\s*$)/g, '') !== ''
            ) {
                _newPar[key] = obj[key]
            }
        }
        // 返回新对象
        return _newPar
    }

    // TODO:多条件查询方法 仅字符串对比 模糊匹配
    const useQuery = (data: any, params: any) => {
        const notNullParams = paramsIsNull(params)
        return data.filter((item) => {
            return Object.keys(notNullParams).every((key: string) => {
                if (key === 'createTime') {
                    const target = item[key] ? item[key].replace('T', ' ') : ''
                    const [start, end] = notNullParams[key]

                    console.log('start--', formatDateTime(start))
                    console.log('end--', formatDateTime(end))
                    console.log('target--', target)

                    const t = new Date(target).getTime()
                    const s = new Date(start).getTime()
                    const e = new Date(end).getTime()
                    return (
                        s <= t && e >= t
                    )
                }
                else {
                    // eslint-disable-next-line unicorn/prefer-includes
                    const flag = item[key] ? item[key].indexOf(notNullParams[key]) !== -1 : false
                    return (
                        item[key] !== undefined
                        && flag
                    )
                }
            })
        })
    }

    // 前端分页
    const usePage = (data: any, gridOptions: any) => {
        const { currentPage, pageSize } = gridOptions.pagerConfig
        const val = cloneDeep(data)
        const list = val.splice((currentPage - 1) * pageSize, pageSize)
        return {
            list,
            total: data.length,
        }
    }

    return { addBtn, btns, gridOptions, usePage, useQuery }
}
